package com.gjs.freechat.web.controller;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.gjs.freechat.server.business.UserManageBusiness;
import com.gjs.freechat.server.component.bean.UserInfo;
import com.gjs.freechat.utils.MyStringUtils;
import com.gjs.freechat.utils.SHA1;
import com.gjs.freechat.web.business.WebUserManage;

@RestController
@RequestMapping("/action/userManage")
public class UserManageController extends AbstractBaseController {
	
	private static Logger logger = Logger.getLogger(UserManageController.class);

	@Autowired
	private UserManageBusiness userManageBusiness;
	
	@Autowired
	private WebUserManage webUserManage;
	
	/**
	 * 接收前端发来得用户名或邮箱，以及密码，然后进行验证
	 * @param email
	 * @param password
	 * @return 返回验证结果
	 */
	@RequestMapping("/login")
	public int login(@RequestParam(name = "email", required=false) String email, 
			@RequestParam(name="password", required=false) String password) {
		
		logger.info(String.format("用户登录,email=%s,password=%s", email, password));
		
		int resultCode;
		if (MyStringUtils.isBlank(email)) {
			resultCode = webUserManage.unregisterLogin(getRequest(), getResponse());
		} else {
			resultCode = webUserManage.registeredLogin(email, password, getRequest(), getResponse());
		}
		return resultCode;
	}
	
//	/**
//	 * 自动登录，后台自动产生一个随机的用户信息
//	 * @return
//	 */
//	@RequestMapping("/login")
//	public int login() {
//		int resultCode = webUserManage.unregisterLogin(getRequest(), getResponse());
//		return resultCode;
//	}
	
	@RequestMapping("/logout")
	public int logout() {
		webUserManage.removeUserCookie(getRequest(), getResponse());
		return webUserManage.logout(getRequest(), getResponse());
	}
	
	@RequestMapping("/leave")
	public int leave() {
		return webUserManage.logout(getRequest(), getResponse());
	}
	
	@RequestMapping("/getUserInfo")
	public UserInfo getUserInfo() {
		return webUserManage.getLoginedUserInfo(getRequest());
	}
	
	@RequestMapping("/updateUserInfo")
	public void updateUserInfo(@RequestBody UserInfo userInfo) {
		if (userInfo == null)
			return;
		webUserManage.updateUserInfo(userInfo, getRequest());
		userManageBusiness.updateUserInfo(userInfo);
	}
	
	@RequestMapping("/userRegister")
	public int userRegister(@RequestParam("userId") String userId, 
			@RequestParam("username") String username, @RequestParam("email") String email, 
			@RequestParam("password") String password) {
		password = SHA1.encode(password);
		if (userManageBusiness.userRegister(userId, email, password)) {
			webUserManage.updateCookie(password, WebUserManage.COOKIE_PASSWORD, getRequest(), getResponse());
			return 1;
		}
		return 0;
	}
	
	@RequestMapping("/editUsername")
	public int editUsername(@RequestParam("userId") String userId, @RequestParam("username") String username) {
		if (userManageBusiness.editUsername(userId, username)) {
			webUserManage.updateCookie(username, WebUserManage.COOKIE_USERNAME, getRequest(), getResponse());
			return 1;
		}
		return 0;
	}
	
	@RequestMapping("/editPassword")
	public void editPassword(@RequestParam("userId") String userId, @RequestParam("newPassword") String newPass) {
		newPass = SHA1.encode(newPass);
		userManageBusiness.editPassword(userId, newPass);
		webUserManage.updateCookie(newPass, WebUserManage.COOKIE_PASSWORD, getRequest(), getResponse());
	}
	
	@RequestMapping("/test")
	public void test() {
		System.out.println(this + ".test");
	}
	
}
